<?php
define("_needlogin", "0");
define("_access", "1");
include_once("include/db.config.php");
if (isset($_POST['getaccess'])) {
    $verify=$_POST['verify'];
    $verify_=$_SESSION['verify'];
    if(!isset($verify)||$verify!=$verify_){
        echo '<script charset="UTF-8">alert("验证码错误！");location.href="./register.php";</script>';
        die();
    }
    $is_submit=1;
    if(!(isset($_POST['name']) && isset($_POST['student_id']) && isset($_POST['school']) && isset($_POST['nickname']) && isset($_POST['email']) && isset($_POST['password']) && isset($_POST['repassword']) && isset($_POST['phone'])))
    {
         $is_submit=0;
    }
    if($is_submit==0)
        die();
    $name = mysql_real_escape_string($_POST['name']);
    $student_id = mysql_real_escape_string($_POST['student_id']);
    $school = mysql_real_escape_string($_POST['school']);
    $nickname = mysql_real_escape_string($_POST['nickname']);
    $email = mysql_real_escape_string($_POST['email']);
    $password = mysql_real_escape_string($_POST['password']);
    $repassword = mysql_real_escape_string($_POST['repassword']);
    $phone = mysql_real_escape_string($_POST['phone']);
    $is_not_empty=1;
    if((empty($name) || empty($student_id) || empty($school) || empty($nickname) || empty($email) || empty($password) || empty($repassword) || empty($phone)))
      {
         $is_not_empty=0;
      }
    if($is_not_empty==1){
    	if($password!=$repassword)
    	{
    		echo '<script charset="UTF-8">alert("两次输入的密码不一致");location.href="./register.php";</script>';
    	}
        $is_valid=0;
        $str=array();
        $sql = "SELECT * from member where student_id='$student_id'";
        $r = getOne($sql);
        if($r){
            $is_valid=1;
            $str[0]="该学号";
        }
        else
            $str[0]="";
        $sql = "SELECT * from team where name='$nickname'";
        $r = getOne($sql);
        if($r){
            $is_valid=1;
            $str[1]="该昵称";
        }
        else
            $str[1]="";
        $sql = "SELECT * from team where email='$email'";
        $r = getOne($sql);
        if($r){
            $is_valid=1;
            $str[2]="该登陆邮箱";
        }
        else
            $str[2]="";
        if($is_valid==0){
        	$sql = "INSERT INTO team (name , email ,password , phone , school, captainID ,solves ,score) VALUES ('$nickname' , '$email' ,'$password' ,'$phone' ,'$school' , '$student_id' ,'' , 0)"; 
            $r = getOne($sql);
            $sql ="SELECT * from team where name ='$nickname'";
            $r = getOne($sql);
            $id = $r['id'];
            $sql = "INSERT INTO member (student_id , name , team_id) VALUES ('$student_id' , '$name' , $id)";
            $r = getOne($sql);
            $sql ="SELECT * from team where id=$id";
            $r = getOne($sql);
            $status=$r['status'];
            if($status==0)
                echo '<script charset="UTF-8">alert("注册成功,请等待管理员审核");location.href="./login.php";</script>';
            else
                echo '<script charset="UTF-8">alert("注册成功,可以开始使用");location.href="./login.php";</script>';
        }
        else{
            $str0="";
            for($i=0;$i<=2;$i++)
            {
                if($i!=0&&$str[$i]!="")
                    $str0=$str0."和";
                $str0=$str0.$str[$i];
            }
            $str0=$str0."已被注册";
            echo "<script charset=\"UTF-8\">alert(\"".$str0."\");location.href=\"./register.php\";</script>\"";
        }           
    }
}
?>
<!DOCTYPE html>
<html>
<?php include("include/head.php"); ?>
<link rel="stylesheet" href="source/register.css">
<body>
<?php include("include/nav.php"); ?>
<div class="row login-wrap">
	<div class="row">
	</div>
	<div class="login-form">
		<h1>实名注册</h1>
		<form id="register_form" method="POST" action="register.php">
			<input id="r_name" class="radius" type='text' name='name' placeholder='姓名' onchange="value=checkUsername(value,'#r_name')" onclick="clearThis('#r_name')"><br/>
			<input id="r_num" class="radius" type='text' name='student_id' placeholder='学号' onchange="value=checkNum(value,'#r_num')" onclick="clearThis('#r_num')"><br/>
			<input id="r_school" class="radius" type='text' name='school' placeholder='学校' onchange="value=checkSchoolname(value,'#r_school')" onclick="clearThis('#r_school')"><br/>
			<input id="r_nick" class="radius" type='text' name='nickname' placeholder='昵称' onchange="value=checkNick(value,'#r_nick')" onclick="clearThis('#r_nick')"><br/>
			<input id="r_email" class="radius" type='text' name='email' placeholder='登录邮箱' onchange="value=checkEmail(value,'#r_email')" onclick="clearThis('#r_email')"><br/>
			<input id="r_pass1" class="radius" type='password' name='password' placeholder='密码' onchange="checkPass()" onclick="clearThis('#r_pass1');clearThis('#r_pass2')"><br/>
			<input id="r_pass2" class="radius" type='password' name='repassword' placeholder='确认密码' onchange="checkPass()" onclick="clearThis('#r_pass1');clearThis('#r_pass2')"><br/>
			<input id="r_phone" class="radius" type='text' name='phone' placeholder='手机号码' onchange="value=checkPhonenum(value,'#r_phone')" onclick="clearThis('#r_phone')"><br/>
			<input id="r_verify" class="radius" type='text' name='verify' placeholder='验证码' onclick="clearThis('#r_verify')"><br/>
			<img src="getVerify.php" alt="" onclick="this.src='getVerify.php?'+ new Date().getTime();">
			<p>注册完成后请耐心等待审核</p>
			<button id="button_submit" class="radius button" onclick="submit_form()" type="button">注册</button>
		</form>
	</div>
</div>
<?php include("include/script.php"); ?>
<script src="source/register.js"></script>
</body>
</html>